﻿using Common.Communication;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Common.Tool.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class ShiBoQi : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public ShiBoQiEnum.TestMode mode { get; set; }
        public int 通道 { get; set; }
        public string 电压挡位 { get; set; }
        public string 时基 { get; set; }
        public string 耦合方式 { get; set; }
        public string 带宽限制 { get; set; }
        public int 等待时间 { get; set; }
        /// <summary>
        /// 获取测试指令
        /// </summary>
        /// <param name="deviceName">仪器名称</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case ShiBoQiEnum.TestMode.峰峰值: 
                    order.range = GetRange("V"); 
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 电压挡位, 时基, 耦合方式, 带宽限制, 上限值, 下限值, 等待时间 }); break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试项指令</param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            var device = dev as IShiBoQi;
            var result = new DeviceResult();
            Result<double> r1 = new Result<double>();
            switch (mode)
            {
                case ShiBoQiEnum.TestMode.峰峰值:
                    //峰峰值
                    r1 = device.GetVpp(通道,
                    (ShiBoQiEnum.TimeDiv)Enum.Parse(typeof(ShiBoQiEnum.TimeDiv), 时基),
                    (ShiBoQiEnum.VolDiv)Enum.Parse(typeof(ShiBoQiEnum.VolDiv), 电压挡位),
                    (ShiBoQiEnum.Coup)Enum.Parse(typeof(ShiBoQiEnum.Coup), 耦合方式),
                    (ShiBoQiEnum.Limit)Enum.Parse(typeof(ShiBoQiEnum.Limit), 带宽限制),等待时间);
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    result.value = r1.Value;
                    result.unit = "V";
                    result.result = DataConvert.ToUnit(r1.Value, "F3") + result.unit;
                    break;
                default:
                    break;
            }
            //上下限判断
            if (下限值 != null && result.success)
            {
                result.success = r1.Value >= 下限值;
            }
            if (上限值 != null && result.success)
            {
                result.success = r1.Value <= 上限值;
            }
            return result;
        }
    }
}
